package cn.com.cetccst.mdmdemo.doubleApn;

import android.content.ComponentName;
import android.content.Context;
import android.util.Log;

import com.hihonor.android.app.admin.DeviceNetworkManager;
import com.zdk.mdm.engine.MdmDeviceAdminReceiver;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DoubleApnOpt {
    private static final String TAG = DoubleApnOpt.class.getSimpleName();


    private DeviceNetworkManager mDevNetMgr = null;
    private ComponentName mAdminName = null;
    public Context context;
    public HashMap<String, String> apnInfo;


    public static VPDNState vpdnState = VPDNState.NULL;

    public enum VPDNState {
        CONNECTING, CONNECTED, DISCONNECTING, DIS_CONNECTED, NULL
    }

    DoubleApnOpt() {

    }

    DoubleApnOpt(Context context) {
        this.context = context;
        mDevNetMgr = new DeviceNetworkManager();
        mAdminName = new ComponentName(context.getApplicationContext(), MdmDeviceAdminReceiver.class);

    }


    public void addApn(Map<String, String> apnInfo) {
        mDevNetMgr.addApn(mAdminName, apnInfo);
        Log.e(TAG, "addApn: " + apnInfo.get("name"));
    }

    public void delApn(String apnId) {
        mDevNetMgr.deleteApn(mAdminName, apnId);
        Log.e(TAG, "delApn: " + apnInfo.get("name"));
    }

    public void queryApn(Map<String, String> apnInfo) {
        List<String> apns = mDevNetMgr.queryApn(mAdminName, apnInfo);
        if (apns != null) {
            StringBuilder stringBuilder = new StringBuilder();
            for (String apn : apns) {
                stringBuilder.append(apn + ";");
            }
            Log.e(TAG, "queryApn: " + stringBuilder.toString());
        }
    }

    public void activeApn(String userId, String apnName) {
//        boolean res = mDevNetMgr.activeAPN(mAdminName,userId , apnName);
//        Log.e(TAG, "activeApn: "+apnInfo.get("name")+" is "+res );
    }

    public void setDefaultAPNPkgList(String userId, String apnName, ArrayList<String> pkgList) {
//        try{
//            boolean res = mDevNetMgr.setDefaultAPNPkgList(mAdminName,userId , apnName,pkgList);
//            Log.e(TAG, "clearDefaultApn: "+"apnName is "+res );
//        }catch (SecurityException){
//            Log.e(TAG, "此APK未经设备管理激活或此APK不属于当前用户 ");
//        }catch (IllegalArgumentException){
//            Log.e(TAG, "参数admin为null ");
//        }
    }

    public void clearDefaultApn(String userId, String apnName) {
//        try{
//            boolean res = mDevNetMgr.clearDefaultApn(mAdminName,userId , apnName);
//            Log.e(TAG, "clearDefaultApn: "+"apnName is "+res );
//        }catch (SecurityException){
//            Log.e(TAG, "此APK未经设备管理激活或此APK不属于当前用户 ");
//        }catch (IllegalArgumentException){
//            Log.e(TAG, "参数admin为null ");
//        }
    }

    public void getDefaultAPNPkgList(String userId, String apnName) {
//        try{
//            ArrayList<String> apns = mDevNetMgr.getDefaultAPNPkgList(mAdminName,userId , apnName);
//            if (apns!=null){
//                StringBuilder stringBuilder = new StringBuilder();
//                for (String apn:apns){
//                    stringBuilder.append(apn+";");
//                }
//                Log.e(TAG, "getDefaultAPNPkgList: "+stringBuilder.toString());
//            }
//        }catch (SecurityException){
//            Log.e(TAG, "此APK未经设备管理激活或此APK不属于当前用户 ");
//        }catch (IllegalArgumentException){
//            Log.e(TAG, "参数admin为null ");
//        }
    }


}
